﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Security;
using Csla;

namespace Dapi.Authentication
{
  [Serializable]
  public class DapiIdentity : CslaIdentity
  {
    public static DapiIdentity GetIdentity(string userName, string password)
    {
      return DataPortal.Fetch<DapiIdentity>(new CredentialsCriteria(userName, password));
    }
    public static DapiIdentity GetIdentity(string userName)
    {
      return DataPortal.Fetch<DapiIdentity>(userName);
    }

    private void DataPortal_Fetch(CredentialsCriteria criteria)
    {
      try
      {
        base.Name = "Bob";
        base.IsAuthenticated = true;
        base.AuthenticationType = "DapiCustom";
        base.Roles = new Csla.Core.MobileList<string> { "perm1", "perm2", "perm3" };
      }
      catch(Exception ex)
      {
        base.Name = string.Empty;
        base.IsAuthenticated = false;
        base.AuthenticationType = string.Empty;
        base.Roles = new Csla.Core.MobileList<string>();
      }
    }
    private void DataPortal_Fetch(string username)
    {
      try
      {
        base.Name = "Bob";
        base.IsAuthenticated = true;
        base.AuthenticationType = "DapiCustom";
        base.Roles = new Csla.Core.MobileList<string> { "perm1", "perm2", "perm3" };
      }
      catch(Exception ex)
      {
        base.Name = string.Empty;
        base.IsAuthenticated = false;
        base.AuthenticationType = string.Empty;
        base.Roles = new Csla.Core.MobileList<string>();
      }
    }

    //private void LoadUser(UserDto user)
    //{
    //  if(user != null)
    //  {
    //    base.Name = user.Username;
    //    base.IsAuthenticated = true;
    //    base.AuthenticationType = "DapiCustom";
    //    base.Roles = new Csla.Core.MobileList<string>(user.Permissions);
    //  }
    //  else
    //  {
    //    base.Name = string.Empty;
    //    base.IsAuthenticated = false;
    //    base.AuthenticationType = string.Empty;
    //    base.Roles = new Csla.Core.MobileList<string>();
    //  }
    //}

  }
}
